package edu.vt.ece4564.bluetendance.server;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import edu.vt.ece4564.bluetendance.attendance.AttendanceRegistrationHandler;
import edu.vt.ece4564.bluetendance.attendance.ClassAttendanceGetter;
import edu.vt.ece4564.bluetendance.classes.ClassInfoGetter;
import edu.vt.ece4564.bluetendance.classes.ClassListingGetter;
import edu.vt.ece4564.bluetendance.classes.TeacherRegistrationHandler;
import edu.vt.ece4564.bluetendance.students.BluetoothMappingGetter;
import edu.vt.ece4564.bluetendance.students.UserRegistrationHandler;

@Controller
public class BluetendanceServerController {
	
	@RequestMapping("/getClassesForUser")
	public void getClassesForUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("userName") String userName) throws IOException {
		ClassListingGetter classListingGetter = new ClassListingGetter(response, userName);
		classListingGetter.run();
	}
	
	@RequestMapping("/getClassInfo")
	public void getClassInfo(HttpServletRequest request, HttpServletResponse response, @RequestParam("classID") String classID) throws IOException {
		ClassInfoGetter classInfoGetter = new ClassInfoGetter(response, Integer.parseInt(classID));
		classInfoGetter.run();
	}
	
	@RequestMapping("/registerAttendance")
	public void registerAttendance(HttpServletRequest request, HttpServletResponse response, @RequestParam("classID") String classID, @RequestParam("numberOfStudents") String numberOfStudents) throws IOException {
		String students = request.getHeader("list");
		AttendanceRegistrationHandler attendanceRegistrationHandler = new AttendanceRegistrationHandler(response, Integer.parseInt(classID), students);
		attendanceRegistrationHandler.run();
	}
	
	@RequestMapping("/getBluetoothMapping")
	public void getBluetoothMapping(HttpServletRequest request, HttpServletResponse response, @RequestParam("classID") String classID) throws IOException {
		BluetoothMappingGetter bluetoothMappingGetter = new BluetoothMappingGetter(response, Integer.parseInt(classID));
		bluetoothMappingGetter.run();
	}
	
	@RequestMapping("/getClassAttendance")
	public void getClassAttendance(HttpServletRequest request, HttpServletResponse response, @RequestParam("className") String className, @RequestParam("date") String date) throws IOException {
		ClassAttendanceGetter classAttendanceGetter = new ClassAttendanceGetter(response, className, date);
		classAttendanceGetter.run();
	}
	
	@RequestMapping("/registerUser")
	public void registerUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String userName, @RequestParam("bluetoothName") String bluetoothName, @RequestParam("bluetoothID") String bluetoothID, @RequestParam("className") String className) throws IOException {
		if ((userName == "") || (bluetoothID == "") || (bluetoothName == "") || (className == "")) {
			response.getWriter().write("You must fill out all of the fields to register a new user.");
		}
		else {
			UserRegistrationHandler userRegistrationHandler = new UserRegistrationHandler(response, userName, bluetoothName, bluetoothID, className);
			userRegistrationHandler.run();
		}
	}
	
	@RequestMapping("registerTeacher")
	public void registerTeacher(HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String teacherName, @RequestParam("className") String className) throws IOException {
		if ((teacherName == "") || (className == "")) {
			response.getWriter().write("You must fill out all of the fields to register a new user.");
		}
		else {
			TeacherRegistrationHandler teacherRegistrationHandler = new TeacherRegistrationHandler(response, teacherName, className);
			teacherRegistrationHandler.run();
		}
	}
}
